//
//  SFPBodyRecordController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit

class SFPBodyRecordController: SFPBaseViewController {
    lazy var viewModel: SFPBodyRecordViewModel = {
        return SFPBodyRecordViewModel()
    }()
    @IBOutlet var daysLabel: UILabel!
    @IBOutlet var weightLasLabel: UILabel!
    @IBOutlet var weightPreLabel: UILabel!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var tyreLabel: UILabel!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setTableView()
        // Do any additional setup after loading the view.
    }
    func setTableView(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "SFPBodyRecordListCell", bundle: .main), forCellReuseIdentifier: "SFPBodyRecordListCell")
    }
    func loadData(){
        self.viewModel.loadData { models in
            let model = models.first
            self.weightPreLabel.text = model?.weight.components(separatedBy: ".").first
            self.weightLasLabel.text = "." + (model?.weight.components(separatedBy: ".").last)! + "kg"
            self.tyreLabel.text = model!.tyre + " cm"
            self.daysLabel.text = model!.days + " Days"
            self.tableView.reloadData()
        }
    }
    @IBAction func gotoAdd(_ sender: Any) {
        self.navigationController?.pushViewController(SFPMoreRecordsController(), animated: true)
    }
    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}
extension SFPBodyRecordController:UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SFPBodyRecordListCell = tableView.dequeueReusableCell(withIdentifier: "SFPBodyRecordListCell") as! SFPBodyRecordListCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
}
